/**
Zeke Elder - zbe@andrew.cmu.edu
Amal Chemmany - achemman@andrew.cmu.edu
Nishant Kumar - nkumar1@andrew.cmu.edu
15-637
4/2/2012
 **/

package edu.cmu.fithub.actions;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import edu.cmu.fithub.controller.Action;
import edu.cmu.fithub.db.DBHelper;
import edu.cmu.fithub.db.bean.Comments;
import edu.cmu.fithub.db.bean.DisplayComments;
import edu.cmu.fithub.db.bean.DisplayPosts;
import edu.cmu.fithub.db.bean.Group;
import edu.cmu.fithub.db.bean.GroupMembers;
import edu.cmu.fithub.db.bean.Posts;
import edu.cmu.fithub.db.bean.User;

public class DisplayGroupHomeAction extends Action {

	public DisplayGroupHomeAction() {
	}

	public String getName() {
		return "showGroupHome.do";
	}

	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);

		List<String> errorList = new ArrayList<String>();
		User user = (User) request.getSession().getAttribute("user");
		String groupId = request.getParameter("groupId");
		Session dbSession = DBHelper.getSessionFactory().openSession();
		List list = dbSession.createCriteria(Group.class)
				.add(Restrictions.eq("groupId", Integer.parseInt(groupId)))
				.list();
		if (list.size() != 0) {
			Group group = (Group) list.get(0);
			request.setAttribute("group", group);

			// check user is a member
			List member = dbSession.createCriteria(GroupMembers.class)
					.add(Restrictions.eq("userId", user.getUserId()))
					.add(Restrictions.eq("group.groupId", group.getGroupId()))
					.list();

			// if group is closed and user is not a member
			// don't display member list or posts
			if (member.size() != 0
					|| group.getGroupType().equalsIgnoreCase("open")) {
				List memberList = dbSession
						.createCriteria(GroupMembers.class)
						.add(Restrictions.eq("group.groupId",
								Integer.parseInt(groupId))).list();
				GroupMembers[] members = new GroupMembers[memberList.size()];
				members = (GroupMembers[]) memberList.toArray(members);

				List list2 = dbSession.createCriteria(GroupMembers.class)
						.add(Restrictions.eq("userId", user.getUserId()))
						.list();

				request.setAttribute("memberList", members);

				// retrieve posts for the group
				List<Posts> postList = dbSession
						.createCriteria(Posts.class)
						.add(Restrictions.eq("groupId",
								Integer.parseInt(groupId))).list();
				Collections.sort(postList);
				
				List<DisplayPosts> orderedPostsList = new ArrayList<DisplayPosts>();
				
				for(int i=0; i<postList.size(); i++) {
					Posts posts = postList.get(i);
			        DisplayPosts orderedPosts = new DisplayPosts();
			        orderedPosts.setGroupId(posts.getGroupId());
			        orderedPosts.setPost(posts.getPost());
			        orderedPosts.setPostId(posts.getPostId());
			        orderedPosts.setPostType(posts.getPostType());
			        orderedPosts.setUser(posts.getUser());
			        
			        SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss aaa");
			        df.setTimeZone(TimeZone.getTimeZone(user.getTimeZone()));
			        String date = df.format(posts.getDate());
			        orderedPosts.setDate(date);
			        
					List<Comments> comments = posts.getComments();
					List<DisplayComments> displayComments = new ArrayList<DisplayComments>();
					for(int j=0; j<comments.size(); j++) {
						DisplayComments comments2 = new DisplayComments();
						comments2.setComment(comments.get(j).getComment());
						comments2.setCommentId(comments.get(j).getCommentId());
						comments2.setPost(posts);
						comments2.setUser(comments.get(j).getUser());
						date = df.format(comments.get(j).getDate());
						comments2.setDate(date);
						displayComments.add(comments2);
					}
					orderedPosts.setComments(displayComments);
					orderedPostsList.add(orderedPosts);
				}

				
				
				DisplayPosts[] groupPosts = new DisplayPosts[orderedPostsList.size()];
				groupPosts = (DisplayPosts[]) orderedPostsList.toArray(groupPosts);
				request.setAttribute("groupPosts", groupPosts);
			}
			if (member.size() == 0
					&& group.getGroupType().equalsIgnoreCase("open")) {
				request.setAttribute("groupButton", "join");
			} else if (member.size() != 0
					&& group.getAdminId() != user.getUserId()) {
				request.setAttribute("groupButton", "leave");
			} else if (group.getAdminId() == user.getUserId()
					&& group.getGroupType().equalsIgnoreCase("closed")) {
				request.setAttribute("groupButton", "invite");
			} else {
				request.setAttribute("groupButton", "openGroup");
			}

		} else {
			errorList.add("No such group exists!!");
			request.setAttribute("errorList", errorList);
			return "error.jsp";
		}

		return "groupHomePage.jsp";
	}
}
